<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of login_modal
 *
 * @author zwang
 */
class signup_model extends CI_Model {

    //put your code here
    function checkEmail($email){
        $sql = "SELECT * FROM user_profile WHERE email = '$email'";
        $query = $this->db->query($sql);
        if($query->num_rows() > 0){
            return true;
        }
        return false;
    }
    
    function checkUsername($username){
        $sql = "SELECT * FROM user WHERE username = '$username'";
        $query = $this->db->query($sql);
        if($query->num_rows() > 0){
            return true;
        }
        return false;
    }
    function insertUserProfile($userid, $firstName, $lastName, $email){
        $sql = "INSERT INTO user_profile (userid, firstName, lastName, email) 
            VALUES (" . $this->db->escape($userid) . ", 
                " . $this->db->escape($firstName) . ", 
                    " . $this->db->escape($lastName) . ", 
                    " . $this->db->escape($email) . ")";
        $this->db->query($sql);
        if($this->db->affected_rows() > 0){
            return TRUE;
        }
        return FALSE;
    }
    
    function insertUser($username, $password){
        $password = MD5($password);
        $sql = "INSERT INTO user (username, password, type) 
            VALUES (" . $this->db->escape($username) . ", 
                " . $this->db->escape($password) . ", 
                    " . $this->db->escape(2) . ")";
        $this->db->query($sql);
        if($this->db->affected_rows() > 0){
            return $this->getUserID($username);
        }
        return FALSE;
    }
    
    function getUserID($username){
        $sql = "SELECT * FROM user WHERE username = '$username' LIMIT 1";
        $query = $this->db->query($sql);
        return $query->row()->id;
    }
}

?>
